package com.msos.service.BManagerService;

import com.msos.dao.BManagerDao.BManagerDao;
import com.msos.model.All_Business.All_Business;
import com.msos.model.Message.Message;
import com.msos.model.RespPageBean.RespPageBean;
import com.msos.model.business.Business;
import com.msos.utils.CommonsUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName: BManagerService
 * @Description: TODO
 * @Author: hsx
 * @Date: 2022/4/15 19:34
 * @Version: v1.0
 */
public class BManagerService {
    BManagerDao bManagerDao = new BManagerDao();

    public RespPageBean getAllBusiness(String page, String rows, String sort, String order) {
        Long total = null;
        List<All_Business> list = null;
        try {
            total = bManagerDao.getTotal();
            int offset = Integer.parseInt(page);
            int size = Integer.parseInt(rows);
            list = bManagerDao.getAllBusiness((offset - 1) * size, size, sort, order);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new RespPageBean(total, list);
    }

    public Integer addBusiness(String bname, String bintro, String bimg, String bmoney) {
        try {
            All_Business all_Business = bManagerDao.getBusinessByName(bname);
            if (all_Business != null) {
                return CommonsUtils.EXIST;
            }
            return bManagerDao.addBusiness(bname, bintro, bimg, bmoney);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }

    public Integer deleteBusinessByAid(String aid) {
        if (aid == null) {
            return CommonsUtils.NOTHING;
        }
        String[] aids = aid.split(",");
        int count = 0;
        for (int i = 0; i < aids.length; i++) {
            try {
                bManagerDao.deleteBusinessByAid(Integer.parseInt(aids[i]));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            count++;
        }
        if (count == aids.length) {
            return CommonsUtils.SUCCESS;
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }

    public Integer openBusinessByAid(String aid) {
        try {
            int count = 0;
            String[] aids = aid.split(",");
            for (int i = 0; i < aids.length; i++) {
                String open = "<button id='open_btn' onclick=\"open1()\" disabled>开通</button>";
                String close = "<button id='open_btn' onclick=\"close1()\" >关闭</button>";
                Integer r1 = bManagerDao.updateOpen(aids[i], open);
                Integer r2 = bManagerDao.updateClose(aids[i], close);
                All_Business all_business = bManagerDao.getBusinessByAid(aids[i]);
                Integer r3 = bManagerDao.openBusinessByAid(all_business.getBname(), all_business.getBintro(), all_business.getBimg(), all_business.getBmoney());
                count++;
            }
            return count == aids.length ? CommonsUtils.SUCCESS : CommonsUtils.EXCEPTION_ERROR;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }

    public Integer closeBusinessByAid(String aid) {
        try {
            int count = 0;
            String[] aids = aid.split(",");
            for (int i = 0; i < aids.length; i++) {
                String open = "<button id='open_btn' onclick=\"open1()\" >开通</button>";
                String close = "<button id='open_btn' onclick=\"close1()\" disabled>关闭</button>";
                Integer r1 = bManagerDao.updateOpen(aids[i], open);
                Integer r2 = bManagerDao.updateClose(aids[i], close);
                All_Business all_business = bManagerDao.getBusinessByAid(aids[i]);
                Integer r3 = bManagerDao.closeBusinessByAid(all_business.getBname());
                count++;
            }
            return count == aids.length ? CommonsUtils.SUCCESS : CommonsUtils.EXCEPTION_ERROR;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }

    public Integer updateBusiness(All_Business all_business) {
        try {
            Integer i = bManagerDao.updateBusiness(all_business);
            return i == 1 ? CommonsUtils.SUCCESS : CommonsUtils.EXCEPTION_ERROR;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return CommonsUtils.EXCEPTION_ERROR;
    }
}
